<template>
  <div>
    <div class="head-container">
      <el-form :inline="true" :model="dataForm">
        <el-input
          v-model="dataForm.blurry"
          clearable
          size="small"
          placeholder="请输入你要搜索的内容"
          style="width: 200px"
          class="filter-item"
        />
        <el-date-picker
          v-model="dataForm.createTime"
          type="daterange"
          size="small"
          unlink-panels
          range-separator=":"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          class="date-item"
        >
        </el-date-picker>
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          @click="getDataList()"
          >搜索</el-button
        >
        <el-button
          v-if="isAuth('mnt:log:export')"
          class="filter-item"
          size="mini"
          type="warning"
          :disabled="!dataList.length"
          @click="doExport()"
          >导出</el-button
        >
        <el-button
          v-if="isAuth('mnt:log:delete')"
          class="filter-item"
          type="danger"
          size="mini"
          @click="delAllData()"
          >清空</el-button
        >
      </el-form>
    </div>
    <el-table :data="dataList" style="width: 100%" v-loading="dataListLoading">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="请求方法">
              <span>{{ scope.row.method }}</span>
            </el-form-item>
            <el-form-item label="请求参数">
              <span>{{ scope.row.params }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="requestIp" label="IP" />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="address"
        label="IP来源"
      />
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="browser" label="浏览器" />
      <el-table-column prop="time" label="请求耗时" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.time <= 300">{{ scope.row.time }}ms</el-tag>
          <el-tag v-else-if="scope.row.time <= 1000" type="warning"
            >{{ scope.row.time }}ms</el-tag
          >
          <el-tag v-else type="danger">{{ scope.row.time }}ms</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期" width="180px">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
import { parseTime, downloadFile } from '@/utils'
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const start = new Date();
            const end = new Date();
            end.setTime(end.getTime() + 3600 * 1000 * 24);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      dataForm: {
        blurry: '',
        createTime: []
      },
      dataList: [],
      dataListLoading: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
    }
  },
  activated() {
    this.getDataList();
  },
  methods: {
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/mnt/log/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'blurry': this.dataForm.blurry,
          'createTime': this.dataForm.createTime ? parseTime(this.dataForm.createTime[0], '{y}-{m}-{d}') : null,
          'endTime': this.dataForm.createTime ? parseTime(this.dataForm.createTime[1], '{y}-{m}-{d}') : null
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    //导出
    doExport() {
      this.$http({
        url: this.$http.adornUrl('/mnt/log/download'),
        method: 'get',
        params: this.$http.adornParams({
          'blurry': this.dataForm.blurry,
          'createTime': this.dataForm.createTime ? parseTime(this.dataForm.createTime[0], '{y}-{m}-{d}') : null,
          'endTime': this.dataForm.createTime ? parseTime(this.dataForm.createTime[1], '{y}-{m}-{d}') : null
        }),
        responseType: 'blob'
      }).then(function (res) {
        //console.log(res);
        downloadFile(res.data, '日志数据', 'xlsx')
      })
    },
    //清空
    delAllData() {
      this.$confirm(`确认清空所有操作日志吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/mnt/log/delete'),
          method: 'delete'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    }

  },
}
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 70px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
.demo-table-expand .el-form-item__content {
  font-size: 12px;
}
</style>
